/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.diplomka.datalayer.daoimpl;

import cz.fi.muni.diplomka.common.enumeration.MovementType;
import cz.muni.fi.diplomka.datalayer.dao.IncomingResourcesDao;
import cz.muni.fi.diplomka.datalayer.entity.IncomingResourcesEntity;
import cz.fi.muni.diplomka.common.enumeration.ResourceType;
import cz.muni.fi.diplomka.datalayer.util.DbConnector;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Jakub Kutil
 */
public class IncomingResourcesDaoImpl implements IncomingResourcesDao {

    private Connection con = null;
    private PreparedStatement ps = null;
    private ResultSet rs = null;

    public IncomingResourcesDaoImpl() {
    }

    @Override
    public void addIncomingResources(IncomingResourcesEntity incomingResourcesEntity) {
        if (incomingResourcesEntity == null) {
            throw new IllegalArgumentException("IncomingResourcesEntity can not be null");
        }
        try {
            con = DbConnector.getDBConnection();
            String insert = "INSERT INTO incoming_resources"
                    + "(city_from, city_to, resource_type, resource_count, time_of_arrival, type) VALUES"
                    + "(?,?,?,?,?,?)";
            ps = con.prepareStatement(insert);

            ps.setLong(1, incomingResourcesEntity.getCityFrom());
            ps.setLong(2, incomingResourcesEntity.getCityTo());
            ps.setString(3, incomingResourcesEntity.getResource().toString());
            ps.setInt(4, incomingResourcesEntity.getResourceCount());
            ps.setTimestamp(5, incomingResourcesEntity.getTimeOfArrival());
            ps.setString(6, incomingResourcesEntity.getMovementType().toString());

            ps.executeUpdate();

            rs = ps.getGeneratedKeys();
            rs.next();


        } catch (SQLException ex) {
            Logger.getLogger(IncomingResourcesDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            DbConnector.closeConnection(rs, ps, con);
        }
    }

    @Override
    public void editIncomingResources(IncomingResourcesEntity incomingResourcesEntity) {
        if (incomingResourcesEntity == null) {
            throw new IllegalArgumentException("IncomingResourcesEntity can not be null");
        }
        try {
            con = DbConnector.getDBConnection();
            String update = "UPDATE incoming_resources SET city_id=?, resource_type=?, resource_count=?, time_of_arrival=?, type=? WHERE id= ?";

            ps = con.prepareStatement(update);

            ps.setLong(1, incomingResourcesEntity.getCityFrom());
            ps.setLong(2, incomingResourcesEntity.getCityTo());
            ps.setString(3, incomingResourcesEntity.getResource().toString());
            ps.setInt(4, incomingResourcesEntity.getResourceCount());
            ps.setTimestamp(5, incomingResourcesEntity.getTimeOfArrival());
            ps.setString(6, incomingResourcesEntity.getMovementType().toString());

            ps.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(IncomingResourcesDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            DbConnector.closeConnection(rs, ps, con);
        }
    }

    @Override
    public void deleteIncomingResources(Long id) {
        if (id == null) {
            throw new IllegalArgumentException("ID can not be null");
        }
        try {
            con = DbConnector.getDBConnection();
            String delete = "DELETE FROM incoming_resources WHERE id=?";

            ps = con.prepareStatement(delete);

            ps.setLong(1, id);

            ps.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(IncomingResourcesDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            DbConnector.closeConnection(rs, ps, con);
        }
    }

    @Override
    public List<IncomingResourcesEntity> getAllIncomingResourcesOfCity(Long cityId) {
        if (cityId == null) {
            throw new IllegalArgumentException("CityId can not be null");
        }
        List<IncomingResourcesEntity> incomingResourcesEntityList = new ArrayList<IncomingResourcesEntity>();

        try {
            con = DbConnector.getDBConnection();
            String getAllIncomingResourcesOfPlayer = "SELECT * FROM incoming_resources WHERE city_to=?";

            ps = con.prepareStatement(getAllIncomingResourcesOfPlayer);
            ps.setLong(1, cityId);

            rs = ps.executeQuery();

            while (rs.next()) {
                IncomingResourcesEntity incomingResourcesEntity = new IncomingResourcesEntity();
                incomingResourcesEntity.setId(rs.getLong("id"));
                incomingResourcesEntity.setCityFrom(rs.getLong("city_from"));
                incomingResourcesEntity.setCityTo(rs.getLong("city_to"));
                incomingResourcesEntity.setResource(ResourceType.valueOf(rs.getString("resource_type")));
                incomingResourcesEntity.setResourceCount(rs.getInt("resource_count"));
                incomingResourcesEntity.setTimeOfArrival(rs.getTimestamp("time_of_arrival"));
                incomingResourcesEntity.setMovementType(MovementType.valueOf(rs.getString("type")));
                incomingResourcesEntityList.add(incomingResourcesEntity);
            }
        } catch (SQLException ex) {
            Logger.getLogger(IncomingResourcesDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            DbConnector.closeConnection(rs, ps, con);
        }
        return incomingResourcesEntityList;
    }

    @Override
    public List<IncomingResourcesEntity> getAllOutcomingResourcesOfCity(Long cityId) {
        if (cityId == null) {
            throw new IllegalArgumentException("CityId can not be null");
        }
        List<IncomingResourcesEntity> incomingResourcesEntityList = new ArrayList<IncomingResourcesEntity>();

        try {
            con = DbConnector.getDBConnection();
            String getAllIncomingResourcesOfPlayer = "SELECT * FROM incoming_resources WHERE city_from=?";

            ps = con.prepareStatement(getAllIncomingResourcesOfPlayer);
            ps.setLong(1, cityId);

            rs = ps.executeQuery();

            while (rs.next()) {
                IncomingResourcesEntity incomingResourcesEntity = new IncomingResourcesEntity();
                incomingResourcesEntity.setId(rs.getLong("id"));
                incomingResourcesEntity.setCityFrom(rs.getLong("city_from"));
                incomingResourcesEntity.setCityTo(rs.getLong("city_to"));
                incomingResourcesEntity.setResource(ResourceType.valueOf(rs.getString("resource_type")));
                incomingResourcesEntity.setResourceCount(rs.getInt("resource_count"));
                incomingResourcesEntity.setTimeOfArrival(rs.getTimestamp("time_of_arrival"));
                incomingResourcesEntity.setMovementType(MovementType.valueOf(rs.getString("type")));
                incomingResourcesEntityList.add(incomingResourcesEntity);
            }
        } catch (SQLException ex) {
            Logger.getLogger(IncomingResourcesDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            DbConnector.closeConnection(rs, ps, con);
        }
        return incomingResourcesEntityList;
    }
    
    @Override
    public List<IncomingResourcesEntity> getAllIncomingResources() {

        List<IncomingResourcesEntity> incomingResourcesEntityList = new ArrayList<>();

        try {
            con = DbConnector.getDBConnection();
            String getAllIncomingResourcesOfPlayer = "SELECT * FROM incoming_resources";

            ps = con.prepareStatement(getAllIncomingResourcesOfPlayer);

            rs = ps.executeQuery();

            while (rs.next()) {
                IncomingResourcesEntity incomingResourcesEntity = new IncomingResourcesEntity();
                incomingResourcesEntity.setId(rs.getLong("id"));
                incomingResourcesEntity.setCityFrom(rs.getLong("city_from"));
                incomingResourcesEntity.setCityTo(rs.getLong("city_to"));
                incomingResourcesEntity.setResource(ResourceType.valueOf(rs.getString("resource_type")));
                incomingResourcesEntity.setResourceCount(rs.getInt("resource_count"));
                incomingResourcesEntity.setTimeOfArrival(rs.getTimestamp("time_of_arrival"));
                incomingResourcesEntity.setMovementType(MovementType.valueOf(rs.getString("type")));
                incomingResourcesEntityList.add(incomingResourcesEntity);
            }
        } catch (SQLException ex) {
            Logger.getLogger(IncomingResourcesDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            DbConnector.closeConnection(rs, ps, con);
        }
        return incomingResourcesEntityList;
    }
}
